﻿package org.openChart.display  
{
	import flash.events.KeyboardEvent;
	import org.openChart.meta.TextLabel
	import flash.geom.Point;
	import org.openChart.util.Rectangle;
	/**
	 * ...
	 * @author Chongyuan
	 */
	public class DisplayTextLabel extends DisplayBaseObject implements DisplayInterface
	{
		private var text_:String = "";
		//private var vertical_:Boolean = false;
		private var bold_:Boolean = false;
		private var rotation_:Number = 0;
		private var textSize_:uint = 16;
		// totally nine types.center, center-left, center-right, center-top, center-bottom, top-left, top-right, bottom-left, bottom-right
		private var align_:String = "center";
		private var label_:TextLabel;
		private var isShow_:Boolean = true;
		
		private var width_:Number;
		private var height_:Number;
		
		private var maxWidth_:Number = Number.MAX_VALUE;
		private var maxHeight_:Number = Number.MAX_VALUE;
		
		public function DisplayTextLabel( label:TextLabel ) 
		{
			if (label)
			{
				this.label_ = label;
				this.text = label.text;
			}
			super(label);
			
		}
		
		/**
		 * Initialize the text label.
		 */
		public override function initialize():void
		{
			super.initialize();
			
			this.text = label.text;
			this.textSize = label.getStyle().fontSize;
			this.bold = label.getStyle().bold;
			initSize();
		}
		
		/**
		 * Calculate the size of text label.
		 */
		public function initSize():void
		{
			var textWidth:Number = 0;
			var textHeight:Number = textSize_;
			var textValue:String = "";
			
			if (text)
			{
				//trace(text);
				for (var i:uint = 0; i < text.length; i++)
				{
					if (text.charCodeAt(i) != 10)
					{
						if (text.charCodeAt(i) < 0xFF)
						{
							textWidth += textSize_ / 2;
							if (textWidth > maxWidth_)
							{
								textWidth =  textSize_ / 2;
								textValue += "\n";
								//textHeight += textSize_ * 2;
							}
							textValue += text.charAt(i);
						}
						else
						{
							textWidth += textSize_;
							if (textWidth > maxWidth_)
							{
								textWidth =  textSize_ / 2;
								textValue += "\n";
								//textHeight += textSize_ * 2;
							}
							textValue += text.charAt(i);
						}
					}
				}
			}
			width_ = textWidth;
			height_ = textHeight;
			//height_ += 5;
		}
		
		public override function resize(rectangle:Rectangle):void
		{
			super.resize(rectangle);
		}
		
		
		public function set text( text_:String ):void
		{
			this.text_ = text_;
		}
		
		public function get text():String
		{
			return text_;
		}
		
		public function get rotation():Number
		{
			return rotation_;
		}
		
		public function set rotation(rotation_:Number):void
		{
			this.rotation_ = rotation_;
		}
		
		public function set bold( bold_:Boolean):void
		{
			this.bold_ = bold_;
		}
		
		public function get bold():Boolean
		{
			return this.bold_;
		}
		
		public function set textSize( textSize_:uint ):void
		{
			this.textSize_ = textSize_;
		}
		
		public function get textSize():uint
		{
			return this.textSize_;
		}
		
		public function set align( align_:String ):void
		{
			this.align_ = align_;
		}
		
		public function get align():String
		{
			return this.align_;
		}
		
		public function get label():TextLabel 
		{
			return this.label_;
		}
		
		public function get width():Number
		{
			return this.width_;
		}
		
		public function get height():Number
		{
			return this.height_;
		}
		
		public function set maxHeight( maxHeight_:Number ):void
		{
			this.maxHeight_ = maxHeight_;
		}
		
		public function get maxHeight():Number
		{
			return maxHeight_;
		}
		
		public function set maxWidth( maxWidth_:Number ):void
		{
			this.maxWidth_ = maxWidth_;
		}
		
		public function get maxWidth():Number
		{
			return maxWidth_;
		}
		
		public function toString():String
		{
			var str:String = "\nObject:DisplayTextLabel ";
			str += "[text = " + text + "]";
			str += "[height = " + height + "]";
			str += "[width = " + width + "]";
			str += "[textSize = " + textSize + "]";
			str += "[align = " + align + "]";
			str += "[rotation = " + rotation + "]";
			str += "[position: x = " + position.x + ", y = " + position.y + "]";
			return str;
		}
		
	}
	
}